<?php
namespace Home\Controller;
use Think\Controller;
require_once ROOT . "/xunsearch/php/lib/XS.php";

class ListController extends BaseController {

	/**
	 * 搜索结果
	 * @param string $keyword 关键字
	 * @param string $page 页码
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function index(){
		$keyword = I('get.keyword');
		$keyword = str_replace("q=", "", $keyword);
		$keyword = mb_substr($keyword, 0, 192); // 限制keyword长度，防止抛出Data/Name too long错误
		//$keyword = str_replace("-", " ", $keyword);
		$page = I('get.page');
		// base url
		$baseUrl = '/' . strtolower(CONTROLLER_NAME) . '/' . urlencode($keyword);

		try {
			$xs = new \XS('codeit');
			$search = $xs->search;
			$search->setCharset('UTF-8');

			if (empty($keyword)) {
				$hot = $search->getHotQuery();
			} else {
				// fuzzy search
				$search->setFuzzy(true);

				// synonym search
				$search->setAutoSynonyms(true);

				// Set query
				$search->setQuery($keyword);

				// set sort order by page rank desc
				//$search->setSort("pageRank", false);

				// set offset, limit
				$page = max(1, intval($page));
				$page = min(100, intval($page));
				$pageNumber = \XSSearch::PAGE_SIZE;
				$search->setLimit($pageNumber, ($page - 1) * $pageNumber);

				// get the result
				$key = "List_".md5("codeit.cn/list/{$keyword}/{$page}");
				$cache = S($key);
				$search_begin = microtime(true);
				if (!empty( $cache ) ) {
					$docs = $cache;
					$count = S($key."count");
					$total = S($key."total");
				} else {
					$docs = $search->search();
					// get other result
					$count = $search->getLastCount();
					$total = $search->getDbTotal();

					S($key, $docs);
					S($key."count", $count);
					S($key."total", $total);
				}
				$search_cost = microtime(true) - $search_begin;
				cookie("SearchCostTime", $search_cost);


				/*
				// try to corrected, if resul too few
				if ($count < 1 || $count < ceil(0.001 * $total)) {
					$corrected = $search->getCorrectedQuery();
				}
				// get related query
				$related = $search->getRelatedQuery();
				*/

				// gen pager
				if ($count > $pageNumber) {
					$pagerBegin = max($page - 5, 1);
					$pagerEnd = min($pagerBegin + 10, ceil($count / $pageNumber) + 1);
					$pager = '';
					do {
						$pager .= ($pagerBegin == $page) ? "		<li class=\"disabled\"><a>{$page}</a></li>\r\n" : "		<li><a href=\"{$baseUrl}/{$pagerBegin}\">{$pagerBegin}</a></li>\r\n";
					} while (++$pagerBegin < $pagerEnd);
				}

				// display
				$this->assign('total', $total);
				$this->assign('keyword', $keyword);
				$this->assign('hot', $hot);
				$this->assign('list', $docs);
				$this->assign('cost', number_format($search_cost, 2));
				$this->assign('count', $count);
				$this->assign('pager', $pager);
				$this->display();

			}
			
		} catch (XSException $e) {
			
		}
		//$this->display();
	}

	/**
	 * 热门关键词
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function hotWords() {
		try {
			$xs = new \XS('codeit');
			$search = $xs->search;
			$search->setCharset('UTF-8');

			$hot = $search->getHotQuery(50);
			//var_dump($hot);
			$this->assign("list", $hot);
			$this->display();
		} catch (XSException $e) {
				
		}
	}

	/**
	 * 精选
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function best() {
		//模型
		$model = D('Best');
		$this->show("<h1>精选内容</h1>");
	}

	/**
	 * 教程
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function howto() {
		//模型
		$model = D('Howto');
		$this->show("<h1>教程</h1>");
	}

	/**
	 * 随机展示20条
	 * @access public
	 * @since 2014-10-20 20:37:13
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function explorer() {
		//模型
		$Hash = D('Hash');

		// 随机数据
		$redis= new \Redis();
		$redis->connect('115.28.215.209',6379);
		$randMember = $redis->sRandMember('maindb',20);
		//var_dump($randMember);
		
		$map['hash'] = array('in', $randMember);
		$members = $Hash->where($map)->field('hash,title,file,fileNumber,fileSize,hot')->select();
		$this->assign("list", $members);
		//var_dump($members);

		$this->display();
	}
}